from rest_framework.serializers import Serializer
import time

from gesture_demo_server.models import User


def now(needing_format: str = '%Y-%m-%d %H:%M:%S') -> str:
    """ 获取当前时间的格式化字符串 """
    return time.strftime(needing_format, time.localtime())


def do_verified_saving(data: Serializer, user: User):
    """ 验证并保存序列化数据数据 """
    rs = {'status': True,
          'data': None
          }
    if data.is_valid():
        result = data.save(**{
            'creator': user,
            'modified': user,
        })
        rs['data'] = data.to_representation(result)
    return rs
